<template>
	<div class="wrapper">
		<!-- header部分 -->
		<header>
			<i @click="togoBack">&#8249;</i>
			<p>商家信息</p>
		</header>
		<!-- 商家logo部分 -->
		<div class="business-logo">
			<img :src="business.businessImg">
		</div>
		<!-- 商家信息部分 -->
		<div class="business-info">
			<h1>{{business.businessName}}</h1>
			<p>&#165;{{business.starPrice}}起送 &#165;{{business.deliveryPrice}}配送</p>
			<p>{{business.businessExplain}}</p>
		</div>
		<div class="edit-business-info">
			<button @click="toEditBusinessInfo">修改商家信息</button>
		</div>
		<!-- 食品列表部分 -->
		<ul class="food">
			<li v-for="item in foodArr" :key="item.foodId">
				<div class="food-left">
					<img :src="item.foodImg">
					<div class="food-left-info">
						<h3>{{item.foodName}}</h3>
						<p>{{item.foodExplain}}</p>
						<p>&#165;{{item.foodPrice}}</p>
					</div>
				</div>

				<div class="food-actions">
					<button @click="toeditFood(item.foodId)">编辑</button>
					<!-- 删除按钮 -->
					<button class="delete-button" @click="deleteFood(item.foodId)">删除</button>
				</div>
			</li>
		</ul>
		<!-- 添加新食品按钮 -->
		<div class="add-food-button">
			<button @click="toAddFood">增加新食品</button>
		</div>
		<!-- 退出登录按钮 -->
		<div class="logout-button">
			<button @click="logout">退出登录</button>
		</div>
	</div>
</template>

<script>
	export default {
		name: 'BusinessManagement',
		data() {
			return {
				businessId: this.$route.query.businessId,
				business: {},
				foodArr: [],
				user: {}
			}
		},
		created() {
			this.user = this.$getSessionStorage('user');
			//根据businessId查询商家信息
			this.$axios.get('Business/BusinessId', {
				params: {
					businessId: this.businessId
				}
			}).then(response => {
				this.business = response.data.result;
			}).catch(error => {
				console.error(error);
			});
			//根据businessId查询所属食品信息
			this.$axios.get('Food/FoodList', {
					params: {
						businessId: this.businessId
					}
				})
				.then(response => {
					this.foodArr = response.data.result;
					for (let i = 0; i < this.foodArr.length; i++) {
						this.foodArr[i].quantity = 0;
					}
					//如果已登录，那么需要去查询购物车中是否已经选购了某个食品
				}).catch(error => {
					console.error(error);
				});
		},
		methods: {
			togoBack() {
				// 返回上一页
				this.$router.go(-1);
			},
			toEditBusinessInfo() {
				this.$router.push({
					path: '/editBusinessInfo',
					query: {
						businessId: this.businessId
					} // 传递商家ID
				});
			},
			deleteFood(foodId) {
				this.$axios.delete(`Food/FoodId`, {
						params: {
							foodId: foodId
						} // 传递foodId
					})
					.then(response => {
						if (response.data.result > 0) {
							alert('删除食物成功');
							// 删除成功后可以重新加载食物列表
						} else {
							alert('删除失败');
						}
					})
					.catch(error => {
						console.error('删除食物失败', error);
					});
			},
			// 跳转到编辑食物信息的页面
			toeditFood(foodId) {
				this.$router.push({
					path: '/editFood',
					query: {
						foodId: foodId
					} // 传递 foodId
				});
			},
			toAddFood() {
				this.$router.push({
					path: '/addFood',
					query: {
						businessId: this.businessId
					}
				});
			},
			logout() {
				// 清除用户信息
				this.$removeSessionStorage('business');
				// 跳转到登录页面
				this.$router.push('/businessLogin');
			}
		}
	}
</script>

<style scoped>
	/****************** 总容器 ******************/
	.wrapper {
		width: 100%;
		height: 100%;
		position: relative;
		padding-bottom: 40vw;
		/* 给底部留空间，防止内容被按钮遮挡 */
	}

	/****************** header部分 ******************/
	.wrapper header {
		width: 100%;
		height: 12vw;
		background-color: #0097FF;
		color: #fff;
		font-size: 4.8vw;

		position: fixed;
		left: 0;
		top: 0;
		z-index: 1000;

		display: flex;
		align-items: center;
	}

	.wrapper header i {
		font-size: 10vw;
		user-select: none;
	}

	.wrapper header p {
		margin-left: 35vw;
	}

	/****************** 商家logo部分 ******************/
	.wrapper .business-logo {
		width: 100%;
		height: 35vw;
		/*使用上外边距避开header部分*/
		margin-top: 12vw;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.wrapper .business-logo img {
		width: 40vw;
		height: 30vw;
		border-radius: 5px;
	}

	/****************** 商家信息部分 ******************/
	.wrapper .business-info {
		width: 100%;
		height: 20vw;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
	}

	.wrapper .business-info h1 {
		font-size: 5vw;
	}

	.wrapper .business-info p {
		font-size: 3vw;
		color: #666;
		margin-top: 1vw;
	}

	/****************** 修改商家信息按钮 ******************/
	.edit-business-info {
		display: flex;
		justify-content: center;
		margin: 2vw 0;
	}

	.edit-business-info button {
		width: 60%;
		padding: 3vw;
		background-color: #0097EF;
		color: white;
		border: none;
		border-radius: 5px;
		font-size: 4vw;
		cursor: pointer;
	}

	.edit-business-info button:hover {
		background-color: #007bce;
	}

	/****************** 食品列表部分 ******************/
	.wrapper .food {
		width: 100%;
		/*使用下外边距避开footer部分*/


	}

	.wrapper .food li {
		width: 100%;
		box-sizing: border-box;
		padding: 2.5vw;
		user-select: none;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.wrapper .food li .food-left {
		display: flex;
		align-items: center;
	}

	.wrapper .food li .food-left img {
		width: 20vw;
		height: 20vw;
	}

	.wrapper .food li .food-left .food-left-info {
		margin-left: 3vw;
	}

	.wrapper .food li .food-left .food-left-info h3 {
		font-size: 3.8vw;
		color: #555;
	}

	.wrapper .food li .food-left .food-left-info p {
		font-size: 3vw;
		color: #888;
		margin-top: 2vw;
	}

	.food-actions {
		display: flex;
	}

	.food-actions button {
		margin-left: 1vw;
		padding: 1.5vw 3vw;
		font-size: 3vw;
		color: white;
		background-color: #0097EF;
		border: none;
		border-radius: 5px;
		cursor: pointer;
	}

	.food-actions .delete-button {
		background-color: #FF4D4F;
	}

	.food-actions .delete-button:hover {
		background-color: #d9363e;
	}

	/****************** 增加食物按钮样式 ******************/
	.add-food-button {
		display: flex;
		justify-content: center;
		margin: 2vw 0;
	}

	.add-food-button button {
		width: 60%;
		padding: 3vw;
		background-color: #38CA73;
		color: white;
		border: none;
		border-radius: 5px;
		font-size: 4vw;
		cursor: pointer;
	}

	.add-food-button button:hover {
		background-color: #2CA65F;
	}

	/****************** 退出登录按钮样式 ******************/
	.logout-button {
		display: flex;
		justify-content: center;
		margin: 2vw 0;
	}

	.logout-button button {
		width: 60%;
		padding: 3vw;
		background-color: #ff4d4f;
		color: white;
		border: none;
		border-radius: 5px;
		font-size: 4vw;
		cursor: pointer;
	}

	.logout-button button:hover {
		background-color: #d9363e;
	}
</style>